<?php

namespace app\models;

use Yii;
use yii\db\Query;
/**
 * This is the model class for table "gky_qyh_sp_authorization".
 *
 * @property integer $id
 * @property integer $proposer_id
 * @property integer $approver_id
 * @property string $create_time
 * @property string $agree_time
 * @property integer $status
 * @property string $print_code
 * @property string $time
 * @property string $authorize_dept
 * @property string $authorize_person
 * @property string $job
 * @property string $reason
 * @property string $signature
 */
class GkyQyhSpAuthorization extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_qyh_sp_authorization';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'proposer_id', 'approver_id', 'create_time', 'status', 'print_code', 'time', 'authorize_dept', 'authorize_person', 'job', 'reason'], 'required'],
            [[ 'proposer_id', 'approver_id', 'status'], 'integer'],
            [['create_time', 'agree_time', 'time'], 'safe'],
            [['print_code', 'authorize_dept', 'authorize_person', 'job', 'reason'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'proposer_id' => 'Proposer ID',
            'approver_id' => 'Approver ID',
            'create_time' => 'Create Time',
            'agree_time' => 'Agree Time',
            'status' => 'Status',
            'print_code' => 'Print Code',
            'time' => 'Time',
            'authorize_dept' => 'Authorize Dept',
            'authorize_person' => 'Authorize Person',
            'job' => 'Job',
            'reason' => 'Reason',
        ];
    }
    
    public static function getAuthorization($printCode) {
        return (new Query())
                        ->select('u.avatar,u.name,l.*,ul.signature')
                        ->from('gky_qyh_sp_authorization as l')
                        ->leftJoin('gky_qyh_user as u','l.proposer_id=u.id')
                        ->leftJoin('gky_qyh_user as ul','l.approver_id=ul.id')
                        ->where(['print_code' => $printCode])
                        ->one();
    }
    
    public static function agreeAuthorization($printCode,$opinion){
        $model= GkyQyhSpAuthorization::findOne(['print_code'=>$printCode]);
        $model->status=1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
    public static function refuseAuthorization($printCode,$opinion){
        $model= GkyQyhSpAuthorization::findOne(['print_code'=>$printCode]);
        $model->status=-1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
    
    
    
}
